<template>
	<view>
		<view class="signBox">
			<c-sign ref="cSign" :lineColor="lineColor" :lineWidth="lineWidth" :bgColor='bgColor'></c-sign>
		</view>
		<view class="tip">请确认维修完成并在上方手写"确认收货"，并签字</view>
		<view class="action">
			<view class="wq-btn" @tap="save">确认收货并签字</view>
		</view>
	</view>
</template>

<script>
	import api from '@/api.js'
	export default {
		data() {
			return {
				id: 0,
				url: '',
				imgUrl:'',
				bgColor: '#fff',
				lineColor: '#000000',
				lineWidth: 4,
			}
		},
		onLoad(e){
			this.id=e.id;
		},
		onBackPress(options) {
					let pages = getCurrentPages() // 上一个页面实例对象   
					let beforePage = pages[pages.length - 2]
					beforePage.$vm.reloadCurrentList(); 
		},
		onUnload(){
			let pages = getCurrentPages() // 上一个页面实例对象
			let beforePage = pages[pages.length - 2]
			beforePage.$vm.reloadCurrentList();
		},
		methods: {
			save() {
				var that=this;
				this.$refs.cSign.save().then(res => {
					
					let baseUrl = 'https://up-z2.qiniup.com/';
					var tempFilePath=res.tempFilePath;
					let key = 'sjfkdgfhjkhjksdjkjhkfdsq' + Date.now();
					let param = {
						file_name: key,
						type: 1
					};
					var token = '';
					api.doRequest('post', 'upload/upload_token', param).then(res => {
						if (res.statusCode == 200) {
							console.log(res.data.data.token);
							token = res.data.data.token;
							uni.uploadFile({
								url: baseUrl, //仅为示例，非真实的接口地址
								filePath: tempFilePath, //请求图片数组
								name: 'file',
								formData: {
									'key': key,
									'token': token,
								}, //请求参数
								success: (uploadFileRes) => {
									var ret=JSON.parse(
										uploadFileRes.data);
									that.imgUrl=ret.key;
									api.doRequest('post','valve/trueShou',{id:this.id,imgUrl:this.imgUrl}).then(res=>{
										if(res.statusCode==200){
											uni.showToast({
												title:"操作成功",
												icon:"none"
											})
											setTimeout(function(){
												uni.navigateBack();
											},500);
											
										}
									})
								}
							})
						}
					})
				}).catch(err => {
					uni.showToast({
						title: err.errMsg,
						icon: 'none'
					})
				})
			}
		}
	}
</script>

<style>
	.signBox {
		width: 670upx;
		position: relative;
		border: 1upx solid #ccc;
		margin: 20upx auto;
		background: #eee;
		padding: 20upx;
		border-radius: 20upx;
	}

	.tip {
		background: #eee;
		width: 670upx;
		padding: 20upx;
		border-radius: 20upx;
		color: #004ea2;
		margin: 20upx auto;
	}

	.action {
		width: 710upx;
		margin: 0 auto;
	}
</style>